package com.meitianhui.convenitenbuy.module.store.ui.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.Constants;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.base.BaseToolbarActivity;
import com.meitianhui.convenitenbuy.config.InfoProvider;
import com.meitianhui.convenitenbuy.main.model.bean.EventBean;
import com.meitianhui.convenitenbuy.main.model.bean.UserToken;
import com.meitianhui.convenitenbuy.module.store.adapter.StoreOneLotteryAdapter;
import com.meitianhui.convenitenbuy.module.store.contract.StoreOneLotteryContract;
import com.meitianhui.convenitenbuy.module.store.di.DaggerStoreComponent;
import com.meitianhui.convenitenbuy.module.store.di.StoreModule;
import com.meitianhui.convenitenbuy.module.store.model.PayInfoBean;
import com.meitianhui.convenitenbuy.module.store.model.StoreOneLotteryBean;
import com.meitianhui.convenitenbuy.module.store.presenter.StoreOneLotteryPresenter;
import com.meitianhui.convenitenbuy.util.JumpUtil;
import com.meitianhui.convenitenbuy.util.RxBus;
import com.meitianhui.convenitenbuy.util.RxUtils;
import com.meitianhui.convenitenbuy.util.TipUtils;
import com.meitianhui.convenitenbuy.widget.dialog.PasswordDialog;

import javax.inject.Inject;

import rx.functions.Action1;

/**
 * Created by Administrator on 2017/11/24.
 * 一元抽奖
 */

public class OneLotteryActivity extends BaseToolbarActivity implements StoreOneLotteryContract.View {
    private String mStoreId, mStoreName;

    private RecyclerView mRvOneLottery;
    private LinearLayout mLlNone;
    private PasswordDialog passwordDialog;
    @Inject
    StoreOneLotteryPresenter presenter;
    private StoreOneLotteryAdapter mAdapter;
    private StoreOneLotteryBean.ActivityListEntity entity;


    @Override
    protected int setView() {
        return R.layout.activity_one_lottery;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        DaggerStoreComponent
                .builder()
                .appComponent(App.getInstance().getAppComponent())
                .storeModule(new StoreModule(this))
                .build()
                .inject(this);
        mRvOneLottery = getViewById(R.id.rv_one_lottery);
        mLlNone = getViewById(R.id.ll_store_one_lottery_none);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRvOneLottery.setLayoutManager(layoutManager);
        isEnable(false);

        initData();
        initInfo();
    }

    private void initInfo() {
        presenter.getOneLottery(mStoreId);
    }


    private void initData() {
        setTbTitle("一元抽奖");
        mStoreId = getIntent().getStringExtra("store_id");
        mStoreName = getIntent().getStringExtra("store_name");
        setTbMenu(R.mipmap.icon_title_detail);
    }

    @Override
    protected void onRefreshData() {

    }

    @Override
    protected void clickMenu() {
        super.clickMenu();
        if (!InfoProvider.getLoginStatus(App.getInstance())) {
            JumpUtil.jumpToLoginActivity(0, OneLotteryActivity.this, 0, null);
            return;
        }
        Intent intent = new Intent(this, OneLotteryParticipateActivity.class);
//        intent.putExtra("url", mAdapter.getUrlImage().get(entity.getAward_pic_path1()));
//        intent.putExtra("store_Id", mStoreId);
//        intent.putExtra("store_nam+e", mStoreName);

        startActivity(intent);
    }

    @Override
    public void showError(String msg) {

    }

    /**
     * 获取店铺一元购活动
     *
     * @param result
     * @param data
     * @param msg
     */
    @Override
    public void getOneLotteryBack(boolean result, StoreOneLotteryBean data, String msg) {
        if (mAdapter == null) {
            mAdapter = new StoreOneLotteryAdapter(this, data.getActivity_list(), data.getDoc_url());
            mRvOneLottery.setAdapter(mAdapter);
            mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    if (!InfoProvider.getLoginStatus(App.getInstance())) {
                        JumpUtil.jumpToLoginActivity(0, OneLotteryActivity.this, 0, null);
                        return;
                    }
                    entity = data.getActivity_list().get(position);
                    String type = entity.getActivity_type();
                    if (TextUtils.equals(type, "YYY")) {
                        showClickDialog(R.mipmap.store_one_lottery_dialog_1);
                    } else if (TextUtils.equals(type, "GGL")) {
                        showClickDialog(R.mipmap.store_one_lottery_dialog_2);
                    } else if (TextUtils.equals(type, "DSK")) {
                        clickDsk();
                    }
                }
            });
        } else {
            mAdapter.setNewData(data.getActivity_list(), data.getDoc_url());
        }
//        mLlNone.setVisibility(data.getActivity_list().isEmpty() ? View.VISIBLE : View.GONE);
    }

    /**
     * 跳转到定时领页面
     */
    private void clickDsk() {
        Intent intent = new Intent(this, OneLotteryDSKActivity.class);
        intent.putExtra("store_Id", mStoreId);
        intent.putExtra("store_name", mStoreName);
        intent.putExtra("url", mAdapter.getUrlImage().get(entity.getAward_pic_path1()));
        intent.putExtra(Constants.ARGUMENT, entity);
        intent.putExtra("iscondet", true);
        startActivity(intent);
    }


    /**
     * 获取店铺一元购活动失败
     *
     * @param msg
     */
    @Override
    public void payLotteryFail(String msg) {
        TipUtils.showToast(App.getInstance(), msg);
    }

    /**
     * 一元购活动摇一摇和刮刮乐支付成功跳转到具体页面
     */
    @Override
    public void payLotterySuccess(PayInfoBean data) {
        if (TextUtils.equals(entity.getActivity_type(), "YYY")) {
            Intent intent = new Intent(this, OneLotteryYYYActivity.class);
            intent.putExtra(Constants.ARGUMENT, entity);
            intent.putExtra("payInfo", data);
            intent.putExtra("url", mAdapter.getUrlImage().get(entity.getAward_pic_path1()));
            intent.putExtra("store_Id", mStoreId);
            intent.putExtra("store_name", mStoreName);
            intent.putExtra("iscondet", true);
            startActivity(intent);
        } else if (TextUtils.equals(entity.getActivity_type(), "GGL")) {
            Intent intent = new Intent(this, OneLotteryGGLActivity.class);
            intent.putExtra(Constants.ARGUMENT, entity);
            intent.putExtra("payInfo", data);
            intent.putExtra("url", mAdapter.getUrlImage().get(entity.getAward_pic_path1()));
            intent.putExtra("store_Id", mStoreId);
            intent.putExtra("store_name", mStoreName);
            intent.putExtra("iscondet", true);
            startActivity(intent);
        }

        RxBus.get().post(new EventBean(Constants.EVENT_TYPE_REFRESH));
    }

    /**
     * 点击活动item显示参与Dialog
     *
     * @param bgResource
     */
    private void showClickDialog(int bgResource) {
        Dialog dialog = new Dialog(this, R.style.dialog_custom);
        dialog.setContentView(R.layout.dialog_store_one_lottery);
        RelativeLayout rl = (RelativeLayout) dialog.findViewById(R.id.rl_dialog_store_one_lottery);
        rl.setBackgroundResource(bgResource);
        RxUtils.bindEvents(dialog.findViewById(R.id.tv_dialog_store_one_lottery), new Action1<View>() {
            @Override
            public void call(View view) {
                dialog.dismiss();
                checkPay();
            }
        });

        RxUtils.bindEvents(dialog.findViewById(R.id.iv_dialog_store_one_lottery_close), new Action1<View>() {
            @Override
            public void call(View view) {
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    /**
     * 当设置了支付密码或者免密支付
     */
    private void checkPay() {
        UserToken userToken = InfoProvider.getUserToken(App.getInstance());
        if (userToken.getSmall_direct_flag().equals("N") && userToken.getPayment_password_flag().equals("Y")) {
            showPasswordDialog();
        } else if (userToken.getSmall_direct_flag().equals("Y") && Float.parseFloat("1") > Constants.SMALL_AMOUNT && userToken.getPayment_password_flag().equals("Y")) {
            showPasswordDialog();
        } else
            presenter.checkPay(mStoreId, entity, mStoreName, mAdapter.getUrlImage().get(entity.getAward_pic_path1()));
    }

    /**
     * 显示支付密码的dialog
     */
    private void showPasswordDialog() {
        if (passwordDialog == null){
            passwordDialog = new PasswordDialog(this, R.layout.dialog_payment_pwd);
            passwordDialog.setOnDialogPsdCompareListener(new PasswordDialog.OnDialogPsdCompleteListener() {

                @Override
                public void onPasswordCorrect(PasswordDialog dialog) {
                    passwordDialog.dismiss();
                    passwordDialog.clearInput();
                    presenter.checkPay(mStoreId, entity, mStoreName, mAdapter.getUrlImage().get(entity.getAward_pic_path1()));
                }

                @Override
                public void onPasswordError(PasswordDialog dialog, String msg) {
                    TipUtils.showToast(OneLotteryActivity.this, msg);
                    passwordDialog.clearInput();
                }
            });
        }

        passwordDialog.show();
        passwordDialog.setPsdDescription(getString(R.string.home_one_lottery_pay));
        passwordDialog.setPsdContent("1.00");
    }

}
